<template >
  <div>
    <brand></brand>

    <el-card class="Hys_card">
      <el-button type="success" @click="dialogVisible = true">添加用户</el-button>

      <el-table :data="rolelist" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              v-for="(item1,i1) in scope.row.children"
              :key="item1.id"
              :class="['bdbottom',i1===0 ? 'bdtop' : '']"
            >
              <!-- //渲染一级权限 -->
              <el-col :span="5 ">
                <el-tag>{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- //渲染二,三级权限 -->
              <el-col :span="19">
                <!-- 通过for循环 嵌套渲染二级权限 -->
                <el-row
                  v-for="(item2,i2) in item1.children"
                  :key="item2.id"
                  :class="[i2===0 ? 'bdtop':'']"
                >
                  <el-col :span="6">
                    <el-tag type="success"    closable
                      @close="removeRigthById(scope.row,item3.id)">{{item2.authName}}</el-tag>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      closable
                      @close="removeRigthById(scope.row,item3.id)"
                      type="warning"
                      v-for="(item3,i3) in item2.children"
                      :key="item3.id"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="商品id"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row)"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="remove(scope.row.id)"
            ></el-button>
            <el-button type="warning" icon="el-icon-star-off" size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 对话框 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="50%" @closed="reset1">
        <el-form :model="rolesFrom" :rules="rules" ref="rolesFrom" label-width="100px">
          <el-form-item label="角色名称" prop="roleName">
            <el-input v-model="rolesFrom.roleName"></el-input>
          </el-form-item>
          <el-form-item label="角色地址" prop="roleDesc">
            <el-input v-model="rolesFrom.roleDesc"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirms">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import brand from "../Bread";
export default {
  components: {
    brand
  },
  data() {
    return {
      rolelist: [],
      dialogVisible: false,
      rolesFrom: {
        roleName: "",
        roleDesc: ""
      },
      rules: {
        roleName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        roleDesc: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.rolelis();
  },
  methods: {
    async rolelis() {
      let res = await this.$http.get("/roles");
      let { data, meta } = res.data;
      if (meta.status == 200) {
        this.rolelist = data;
      } else {
        return;
      }
    },
    async confirms() {
      this.$refs.rolesFrom.validate(async valid => {
        if (!valid) return false;
        let res = await this.$http.post("roles", this.rolesFrom);
        console.log(res);
        let { data, meta } = res.data;
        if (meta.status == 201) {
          this.rolesFrom = data;
          this.$message.success(meta.msg);
          this.rolelis();
          this.dialogVisible = false;
        } else {
          this.$message.error(meta.msg);
        }
      });
    },
    reset1() {
      this.$refs.rolesFrom.resetFields();
    },
    async removeRigthById(role, rightId) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (confirmResult !== "comfirm") {
        return this.$message.info("取消了删除");
      }
      const { data, res } = await this.$http.delete(
        `roles/${role.id}/rights/${rightId}`
      );
      if (res.meta.status !== 200) {
        return this.$message.error("删除失败");
      }
      this.rolelis();
    }
  }
};
</script>
<style lang="scss" scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-top: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>

